<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>读者管理 - 图书管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
</head>
<body>
<!-- 引入导航栏组件 -->
<nav th:replace="fragments/navbar :: navbar"></nav>

<div class="container mt-4">
    <!-- 页面标题和操作按钮 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <h2>读者管理</h2>
        </div>
        <div class="col-md-6 text-right">
            <a href="/readers/new" class="btn btn-primary">添加读者</a>
        </div>
    </div>

    <!-- 搜索框 -->
    <div class="row mb-4">
        <div class="col-md-8 offset-md-2">
            <form th:action="@{/readers}" method="get" class="form-inline">
                <input type="text" name="keyword" class="form-control flex-grow-1" placeholder="请输入读者姓名搜索">
                <button type="submit" class="btn btn-secondary ml-2">搜索</button>
                <a href="/readers" class="btn btn-outline-secondary ml-2">重置</a>
            </form>
        </div>
    </div>

    <!-- 读者列表表格 -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="thead-dark">
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>邮箱</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!-- 遍历读者列表数据 -->
                    <tr th:each="reader : ${readers}">
                        <td th:text="${reader.id}"></td>
                        <td th:text="${reader.name}"></td>
                        <td th:text="${reader.phone} ?: '无'"></td> <!-- 空值处理 -->
                        <td th:text="${reader.email} ?: '无'"></td> <!-- 空值处理 -->
                        <td>
                            <!-- 状态徽章：正常/禁用 -->
                            <span class="badge"
                                  th:classappend="${reader.active ? 'badge-success' : 'badge-danger'}"
                                  th:text="${reader.active ? '正常' : '禁用'}">
                                    </span>
                        </td>
                        <td>
                            <!-- 操作按钮 -->
                            <a th:href="@{/readers/{id}(id=${reader.id})}" class="btn btn-sm btn-info">查看</a>
                            <a th:href="@{/readers/edit/{id}(id=${reader.id})}" class="btn btn-sm btn-warning">编辑</a>
                            <a th:href="@{/readers/delete/{id}(id=${reader.id})}" class="btn btn-sm btn-danger"
                               onclick="return confirm('确定要删除该读者吗？删除后将无法恢复！')">删除</a>
                        </td>
                    </tr>

                    <!-- 无数据提示 -->
                    <tr th:if="${readers.isEmpty()}">
                        <td colspan="6" class="text-center py-3">
                            <span class="text-muted">暂无读者数据，请点击"添加读者"按钮创建</span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 引入Bootstrap依赖 -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>